package cn.zzuisa.news.repository.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.jsp.tagext.TryCatchFinally;

import cn.zzuisa.news.entity.User;
import cn.zzuisa.news.form.UserEditForm;
import cn.zzuisa.news.repository.UserRepository;
import cn.zzuisa.news.util.JDBCUtil;

/**
 * @ClassName: UserDAO.java
 * @Description: TODO
 * @author zzuisa
 * @version V1.0
 * @Date 2018年9月4日 上午11:16:48
 */
public class UserDAO implements UserRepository {

	@Override
	public User login(String username, String password) {
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		String sql = "select id,username,password,nickname,email,point,role,state,createtime from `news_user` where username = ? and password = ?";
		try {
			prepareStatement = conn.prepareStatement(sql);
			prepareStatement.setString(1, username);
			prepareStatement.setString(2, password);
			resultSet = prepareStatement.executeQuery();
			User user = new User();
			if (resultSet.next()) {
				user.setId(resultSet.getInt("id"));
				user.setUsername(resultSet.getString("username"));
				user.setPassword(resultSet.getString("password"));
				user.setNickname(resultSet.getString("nickname"));
				user.setEmail(resultSet.getString("email"));
				user.setPoint(resultSet.getInt("point"));
				user.setRole(resultSet.getInt("role"));
				user.setState(resultSet.getInt("state"));
				user.setCreatetime(resultSet.getTimestamp("createtime"));
				return user;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				JDBCUtil.close(conn, prepareStatement, resultSet);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User findById(Integer id) {
		// TODO Auto-generated method stub
		Connection conn = JDBCUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet = null;
		int flag = 0;
		String sql = "select id,username,password,nickname,email,point,role,state,createtime from `news_user` where id="
				+ id;
		User user = new User();
		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(sql);
			if (resultSet.next()) {
				user.setNickname(resultSet.getString("nickname"));
				user.setPoint(resultSet.getInt("point"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				JDBCUtil.close(conn, statement, resultSet);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public int addpoint(Integer id) {
		// TODO Auto-generated method stub
		Connection conn = JDBCUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet = null;
		int flag = 0;
		String sql = "UPDATE `news_user` SET point=point+11 where id = " + id;
		try {
			statement = conn.createStatement();
			flag = statement.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				JDBCUtil.close(conn, statement, resultSet);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}

	@Override
	public User findByUsername(String username) {
		// TODO Auto-generated method stub
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int flag = 0;
		String sql = "select * from news_user where `username` = ?";
		User user = null;
		try {
			preparedStatement = conn.prepareStatement(sql);
			preparedStatement.setString(1, username);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				user = new User();
				user.setId(resultSet.getInt("id"));
				user.setUsername(resultSet.getString("username"));
				user.setPassword(resultSet.getString("password"));
				user.setNickname(resultSet.getString("nickname"));
				user.setEmail(resultSet.getString("email"));
				user.setPoint(resultSet.getInt("point"));
				user.setRole(resultSet.getInt("role"));
				user.setState(resultSet.getInt("state"));
				user.setCreatetime(resultSet.getTimestamp("createtime"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				JDBCUtil.close(conn, preparedStatement, resultSet);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public boolean edit(UserEditForm userEditForm) {
		// TODO Auto-generated method stub
		Connection conn = JDBCUtil.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int flag = 0;
		String sql = "UPDATE news_user SET password=? where id = ? and password = ?";
		try {
			preparedStatement = conn.prepareStatement(sql);
			 preparedStatement.setString(1, userEditForm.getNewPassword());
			 preparedStatement.setInt(2, userEditForm.getuId());
			 preparedStatement.setString(3, userEditForm.getPrePassword());
			flag = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				JDBCUtil.close(conn, preparedStatement, resultSet);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag > 0 ? true : false;
	}


	@Override
	public boolean editState(Integer id, Integer key) {
		// TODO Auto-generated method stub
				Connection conn = JDBCUtil.getConnection();
				PreparedStatement preparedStatement = null;
				ResultSet resultSet = null;
				int flag = 0;
				String sql = "UPDATE news_user SET state=? where id = ?";
				try {
					preparedStatement = conn.prepareStatement(sql);
					 preparedStatement.setInt(1,key);
					 preparedStatement.setInt(2, id);
					flag = preparedStatement.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					try {
						JDBCUtil.close(conn, preparedStatement, resultSet);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return flag > 0 ? true : false;
	}

}
